<template>
  <el-dialog
    :title="!dataForm.userId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="dataForm.username" placeholder="用户名" maxlength="20" clearable></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="dataForm.nickname" placeholder="昵称" maxlength="20" clearable></el-input>
      </el-form-item>
      <el-form-item label="真实姓名" prop="realname">
        <el-input v-model="dataForm.realname" placeholder="真实姓名" maxlength="20" clearable></el-input>
      </el-form-item>
      <el-form-item label="性别" size="mini" prop="gender">
        <el-radio-group v-model="dataForm.gender">
          <el-radio :label="1">男</el-radio>
          <el-radio :label="0">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.birthday" style="width: 100%;"></el-date-picker>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="手机号" maxlength="11" clearable></el-input>
      </el-form-item>
      <el-form-item label="身份证" prop="idcard">
        <el-input v-model="dataForm.idcard" placeholder="身份证号码" maxlength="18" clearable></el-input>
      </el-form-item>
      <el-form-item label="用户级别" prop="userLevelId">
        <el-select v-model="dataForm.userLevelId" placeholder="请选择用户级别" style="width: 100%;" clearable>
          <el-option
            v-for="item in userLevels"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="余额" prop="balanceFee">
        <el-input v-model="dataForm.balanceFee" placeholder="账户余额" maxlength="15" clearable></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="dataForm.email" placeholder="邮箱" maxlength="50" clearable></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { isMobile, isIdcard, isEmail, isMoney } from '@/utils/validate'
  export default {
    data () {
      var validateMobile = (rule, value, callback) => {
        if (!isMobile(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      var validateIdcard = (rule, value, callback) => {
        if (value) {
          if (!isIdcard(value)) {
            callback(new Error('身份证格式错误'))
          } else {
            callback()
          }
        } else {
          callback()
        }
      }
      var validateEmail = (rule, value, callback) => {
        if (value) {
          if (!isEmail(value)) {
            callback(new Error('邮箱格式错误'))
          } else {
            callback()
          }
        } else {
          callback()
        }
      }
      var validateMoney = (rule, value, callback) => {
        if (value) {
          if (!isMoney(value)) {
            callback(new Error('金额格式错误'))
          } else {
            callback()
          }
        } else {
          callback()
        }
      }
      return {
        visible: false,
        dataForm: {
          userId: 0,
          username: '',
          nickname: '',
          realname: '',
          gender: '',
          birthday: '',
          mobile: '',
          idcard: '',
          userLevelId: '',
          balanceFee: '',
          email: ''
        },
        dataRule: {
          username: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          userLevelId: [
            { required: true, message: '用户级别不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' },
            { validator: validateMobile, trigger: 'blur' }
          ],
          idcard: [
            { validator: validateIdcard, trigger: 'blur' }
          ],
          email: [
            { validator: validateEmail, trigger: 'blur' }
          ],
          balanceFee: [
            { validator: validateMoney, trigger: 'blur' }
          ]
        },
        userLevels: []
      }
    },
    methods: {
      init (id) {
        this.dataForm.userId = id || 0
        this.$http({
          url: this.$http.adornUrl('/back/userlevel/queryAll'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.userLevels = data.list
          }
        }).then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })
        }).then(() => {
          if (this.dataForm.userId) {
            this.$http({
              url: this.$http.adornUrl(`/back/shopUser/info/${this.dataForm.userId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.username = data.user.username
                this.dataForm.nickname = data.user.nickname
                this.dataForm.realname = data.user.realname
                this.dataForm.gender = data.user.gender
                this.dataForm.birthday = data.user.birthday
                this.dataForm.mobile = data.user.mobile
                this.dataForm.idcard = data.user.idcard
                this.dataForm.userLevelId = data.user.userLevelId
                this.dataForm.balanceFee = data.user.balanceFee
                this.dataForm.email = data.user.email
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/back/shopUser/${!this.dataForm.userId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.userId || undefined,
                'username': this.dataForm.username,
                'nickname': this.dataForm.nickname,
                'realname': this.dataForm.realname,
                'gender': this.dataForm.gender,
                'birthday': this.dataForm.birthday ? new Date(this.dataForm.birthday).getTime() : undefined,
                'mobile': this.dataForm.mobile,
                'idcard': this.dataForm.idcard,
                'userLevelId': this.dataForm.userLevelId,
                'balanceFee': this.dataForm.balanceFee,
                'email': this.dataForm.email
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
